"""
类视图  功能视图
"""
from django.http import JsonResponse
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from djco.base.base_api import BaseApi


# 函数装饰器
def before(f):
    def inner(*args, **kwargs):
        print("before")
        ret = f(*args, **kwargs)
        print("after")
        return ret

    return inner


# 功能视图
@csrf_exempt
def fbv_view(request):
    print(request.body)
    if request.method == 'GET':
        return render(request, 'index.html', {'content': '这是GET请求--fbv', 'title': 'GET请求'})
    elif request.method == 'POST':
        return render(request, 'index.html', {'content': '这是POST请求--fbv', 'title': 'POST请求'})
    else:
        return render(request, 'index.html', {'content': '这是(' + request.method + ')请求--fbv', 'title': request.method})


# 类视图
@method_decorator(before, name='get')  # name 指定哪个方法需要运行 before 函数
class CbvView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'index.html', {'content': '这是GET请求 --cbv', 'title': 'GET请求'})

    def post(self, request, *args, **kwargs):
        return render(request, 'index.html', {'content': '这是POST请求 --cbv', 'title': 'POST请求'})

    @csrf_exempt  # 免除csrf验证
    def dispatch(self, request, *args, **kwargs):
        return super(CbvView, self).dispatch(request, *args, **kwargs)
